﻿using FarmSmartSleep.Common.Domain.EarTage;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.EarsCommands
{
    public class LambEarAllDeletedHander : IRequestHandler<LambEarAllDeletedCommand, Result>
    {

        private readonly ILogger<LambEarAllDeletedHander> logger;
        private readonly IBaseRepository<LambEars> lambEarRepository;

        public LambEarAllDeletedHander(ILogger<LambEarAllDeletedHander> logger, IBaseRepository<LambEars> lambEarRepository)
        {
            this.logger = logger;
            this.lambEarRepository = lambEarRepository;
        }
        /// <summary>
        /// 批量删除产羔耳标信息
        /// </summary>
        /// <param name="request">批量删除产羔耳标信息的编号字符串</param>
        /// <param name="cancellationToken"></param>
        /// <returns>批量删除产羔耳标信息响应对象</returns>
        public async Task<Result> Handle(LambEarAllDeletedCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result();
                string[] delId = request.Ids.Split(',');
                foreach (var item in delId)
                {
                    var info = lambEarRepository.GetAll().FirstOrDefault(c => c.LambEarId == Convert.ToInt32(item));
                    if (info != null) 
                    {
                        info.Deletes = true;
                        result.Code +=  lambEarRepository.Update(info);
                    }
                }
                logger.LogInformation("批量删除产羔耳标信息方法调用成功");
                return result;
            }
            catch (Exception ex )
            {
                logger.LogError("批量删除产羔耳标信息方法调用出错" + ex);
                throw;
            }
        }
    }
}
